﻿using System;
using System.Collections.Generic;
using YArchitech.MEP.Electric.Common;
using HYElectricCommon;
using HYFamilyLibDataManager;
using LampUseFactorManager;

namespace YArchitech.Revit.MEP.Electric.Calculator
{
	public class FindUseFactor
	{
		public string ErrorDescription { get; set; }

		public FindUseFactor(LampData lData, RoomData rData, string versionNumber)
		{
			this.lampData = lData;
			this.roomData = rData;
			this.version = versionNumber;
			this.useFactorTableName = null;
			this.ErrorDescription = string.Empty;
		}

		public FindUseFactor(string tableName, RoomData rData, string versionNumber)
		{
			this.lampData = null;
			this.roomData = rData;
			this.version = versionNumber;
			this.useFactorTableName = tableName;
			this.ErrorDescription = string.Empty;
		}

		public bool GetUseFactor(ref double useFactorValue)
		{
			useFactorValue = 0.0;
			if (!this.InitData())
			{
				return false;
			}
			StuRoot stuRoot = new StuRoot();
			useFactorValue = stuRoot.Cal();
			FindUseFactor.m_CurUseFactor.UseFactor = ((useFactorValue < 0.0) ? 0.0 : useFactorValue);
			return FindUseFactor.m_CurUseFactor.UseFactor > 0.0;
		}

		private bool InitData()
		{
			if (this.roomData == null || (this.lampData == null && string.IsNullOrEmpty(this.useFactorTableName)))
			{
				this.ErrorDescription = "获取灯具利用系数失败！";
			}
			DatabaseType dbType = DatabaseType.Local;
			string connStr = "";
			if (!this.GetDataSource(ref dbType, ref connStr))
			{
				return false;
			}
			int num = -1;
			if (!((this.lampData != null) ? this.GetUseFactorIdByLampGuid(ref num) : this.GetUseFactorIdByName(dbType, connStr, ref num)))
			{
				return false;
			}
			List<DataGroup> list = new List<DataGroup>();
			list = UtilizationFactor.GetFactorList(dbType, connStr, ref num);
			string rcr_Ri = UtilizationFactor.GetRcr_Ri(dbType, connStr, ref num);
			if (list.Count <= 0 || string.IsNullOrEmpty(rcr_Ri))
			{
				return false;
			}
			FindUseFactor.m_UseFactorList.Clear();
			foreach (DataGroup dataGroup in list)
			{
				StuUseFactor stuUseFactor = new StuUseFactor();
				stuUseFactor.Ceiling = Convert.ToDouble(dataGroup.CeiRef);
				stuUseFactor.Wall = Convert.ToDouble(dataGroup.WallRef);
				stuUseFactor.Floor = Convert.ToDouble(dataGroup.GroupRef);
				stuUseFactor.RCR_RI = Convert.ToDouble(dataGroup.Rcr_ri);
				stuUseFactor.UseFactor = Convert.ToDouble(dataGroup.Value);
				FindUseFactor.m_UseFactorList.Add(stuUseFactor);
			}
			bool flag = !(rcr_Ri.Trim() == "0");
			CalRoomParam calRoomParam = new CalRoomParam(this.roomData);
			if (!flag)
			{
				FindUseFactor.m_CurUseFactor.RCR_RI = calRoomParam.RoomRCR();
			}
			else
			{
				FindUseFactor.m_CurUseFactor.RCR_RI = calRoomParam.RoomRI();
			}
			FindUseFactor.m_CurUseFactor.Ceiling = (double)calRoomParam.PCeiling();
			FindUseFactor.m_CurUseFactor.Wall = (double)calRoomParam.PWall();
			FindUseFactor.m_CurUseFactor.Floor = (double)calRoomParam.PFloor();
			FindUseFactor.m_CurUseFactor.UseFactor = 0.0;
			if (MathUtils.IsEqual(FindUseFactor.m_CurUseFactor.Ceiling, 0.0, 0.1) || MathUtils.IsEqual(FindUseFactor.m_CurUseFactor.Wall, 0.0, 0.1) || MathUtils.IsEqual(FindUseFactor.m_CurUseFactor.Floor, 0.0, 0.1))
			{
				FindUseFactor.m_CurUseFactor.Ceiling = 0.0;
				FindUseFactor.m_CurUseFactor.Wall = 0.0;
				FindUseFactor.m_CurUseFactor.Floor = 0.0;
			}
			return true;
		}

		private bool GetDataSource(ref DatabaseType databaseType, ref string connStr)
		{
			databaseType = DatabaseType.Local;
			string str = AssistFunc.GetAppRootPath() + "\\Data\\LampUseFactor.db";
			string text = "Data Source=" + str + ";Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
			connStr = text;
			return true;
		}

		private bool GetUseFactorIdByLampGuid(ref int Id)
		{
			SqliteDatabase sqliteDB = FamilyLibDatabase.GetSqliteDB(SubjectType.Electric, this.version);
			if (sqliteDB == null)
			{
				this.ErrorDescription = "未找到电气族库！";
				return false;
			}
			IFamilyTable familyTable = sqliteDB.FamilyTable;
			if (familyTable == null)
			{
				this.ErrorDescription = "打开电气族库失败！";
				return false;
			}
			DbFamily dbFamily = familyTable[this.lampData.GUID];
			if (dbFamily == null)
			{
				this.ErrorDescription = "未找到选择的灯具族！";
				return false;
			}
			DbExData exData = dbFamily.ExData;
			if (exData == null || exData.Eid1 <= 0)
			{
				this.ErrorDescription = "还未给此灯具族指定利用系数表，请用族库管理工具给此灯具族指定利用系数表！";
				return false;
			}
			Id = exData.Eid1;
			return true;
		}

		private bool GetUseFactorIdByName(DatabaseType dbType, string connStr, ref int Id)
		{
			int eidByLampName = UtilizationFactor.GetEidByLampName(dbType, connStr, this.useFactorTableName);
			if (eidByLampName <= 0)
			{
				this.ErrorDescription = "获取灯具利用系数失败，可能与该灯具关联的利用系数表已经不存在了！";
				return false;
			}
			Id = eidByLampName;
			return true;
		}

		public static List<StuUseFactor> m_UseFactorList = new List<StuUseFactor>();

		public static StuUseFactor m_CurUseFactor = new StuUseFactor();

		private LampData lampData;

		private RoomData roomData;

		private string version;

		private string useFactorTableName;
	}
}
